package com.lc.recovery.ui.activity.recoverymine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.request.WorkerMissionListApi;
import com.lc.recovery.http.response.WorkerMissionListBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.BigDecimalUtils;
import com.lc.recovery.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StandardActivity extends AppActivity {
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.tv_current_weight)
    TextView tvCurrentWeight;
    @BindView(R.id.tv_rewards)
    TextView tvRewards;
    @BindView(R.id.tv_over_quota)
    TextView tvOverQuota;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.tv_p_left_true)
    TextView tvPLeftTrue;
    @BindView(R.id.tv_p_left_false)
    TextView tvPLeftFalse;
    @BindView(R.id.tv_p_center_true)
    TextView tvPCenterTrue;
    @BindView(R.id.tv_p_center_false)
    TextView tvPCenterFalse;
    @BindView(R.id.tv_p_right_true)
    TextView tvPRightTrue;
    @BindView(R.id.tv_p_right_false)
    TextView tvPRightFalse;
    @BindView(R.id.rv_dabiao)
    RecyclerView rvDabiao;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    private List<WorkerMissionListBean.DataBean> dataBeanList = new ArrayList<>();
    private BaseQuickAdapter<WorkerMissionListBean.DataBean, BaseViewHolder> dabiaoadapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_standard;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        worker_mission_list();

        dabiaoadapter = new BaseQuickAdapter<WorkerMissionListBean.DataBean, BaseViewHolder>(R.layout.item_dabiao, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, WorkerMissionListBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_duanwei, bean.getTitle());
                baseViewHolder.setText(R.id.tv_jianjing, bean.getWeights());
                baseViewHolder.setText(R.id.tv_status, bean.getMission());
            }
        };
        rvDabiao.setAdapter(dabiaoadapter);
        GlideApp.with(getActivity())
                .load(SPUtil.get(getContext(), IntentKey.IMAGE, ""))
                .placeholder(R.mipmap.avatar_placeholder_ic)
                .error(R.mipmap.avatar_placeholder_ic)
                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                .into(imgHead);
        ;
    }

    private void worker_mission_list() {
        EasyHttp.post(this)
                .api(new WorkerMissionListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, ""))).request(new HttpCallback<WorkerMissionListBean>(this) {

            @Override
            public void onSucceed(WorkerMissionListBean data) {
                if (data.getCode() == 200) {
                    tvCurrentWeight.setText(data.getNow_weights()+"");
                    tvRewards.setText(data.getMission_money()+"");
                    tvDistance.setText(data.getNext_weights()+"");
                    tvOverQuota.setText(data.getMission_weights()+"");
                    if (data.getMiddle()!=null&&data.getMiddle().size()==3){
                        tvPLeftFalse.setText(data.getMiddle().get(0).getTitle());
                        tvPLeftTrue.setText(data.getMiddle().get(0).getTitle());
                        tvPCenterFalse.setText(data.getMiddle().get(1).getTitle());
                        tvPCenterTrue.setText(data.getMiddle().get(1).getTitle());
                        tvPRightFalse.setText(data.getMiddle().get(2).getTitle());
                        tvPRightTrue.setText(data.getMiddle().get(2).getTitle());
                        if (Double.parseDouble(data.getNow_weights())<Double.parseDouble(data.getMiddle().get(0).getWeights())){
                            tvPLeftFalse.setVisibility(View.VISIBLE);

                        }else {
                            tvPLeftTrue.setVisibility(View.VISIBLE);
                        }
                        if (Double.parseDouble(data.getNow_weights())<Double.parseDouble(data.getMiddle().get(1).getWeights())){
                            tvPCenterFalse.setVisibility(View.VISIBLE);

                        }else {
                            tvPCenterTrue.setVisibility(View.VISIBLE);
                        }
                        if (Double.parseDouble(data.getNow_weights())<Double.parseDouble(data.getMiddle().get(2).getWeights())){
                            tvPRightFalse.setVisibility(View.VISIBLE);
                        }else {
                           tvPRightTrue.setVisibility(View.VISIBLE);

                        }
                      double fenzinum=  Double.parseDouble(data.getNow_weights())-Double.parseDouble(data.getMiddle().get(0).getWeights());
                        double fenmunum=  Double.parseDouble(data.getMiddle().get(2).getWeights())-Double.parseDouble(data.getMiddle().get(0).getWeights());
                        String progress= BigDecimalUtils.mul(BigDecimalUtils.div(fenzinum+"",fenmunum+"",2),"100",0)+"";
                        progressBar.setProgress(Integer.valueOf(progress));
                        dataBeanList.clear();
                        dataBeanList.addAll(data.getData());
                        dabiaoadapter.setNewData(dataBeanList);
                        dabiaoadapter.notifyDataSetChanged();
                    }

                } else {
                    toast(data.getMsg());
                }
            }


            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    @OnClick(R.id.tv_yue)
    public void onClick() {
        startActivity(new Intent(getContext(),BalanceRecordActivity.class).putExtra("user_type","1"));
    }
}
